<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2007 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// $Id$

/**
 +------------------------------------------------------------------------------
 * Think兼容函数库 针对5.2.0以下版本
 +------------------------------------------------------------------------------
 * @category   Think
 * @package  Common
 * @author   liu21st <liu21st@gmail.com>
 * @version  $Id$
 +------------------------------------------------------------------------------
 */

if (!function_exists('json_encode')) {
	function format_json_value(&$value)
	{
		if(is_bool($value)) {
			$value = $value?'true':'false';
		}elseif(is_int($value)) {
			$value = intval($value);
		}elseif(is_float($value)) {
			$value = floatval($value);
		}elseif(defined($value) && $value === null) {
			$value = strval(constant($value));
		}elseif(is_string($value)) {
			$value = '"'.addslashes($value).'"';
		}
		return $value;
	}

	function json_encode($data)
	{
		if(is_object($data)) {
			//对象转换成数组
			$data = get_object_vars($data);
		}else if(!is_array($data)) {
			// 普通格式直接输出
			return format_json_value($data);
		}
		// 判断是否关联数组
		if(empty($data) || is_numeric(implode('',array_keys($data)))) {
			$assoc  =  false;
		}else {
			$assoc  =  true;
		}
		// 组装 Json字符串
		$json = $assoc ? '{' : '[' ;
		foreach($data as $key=>$val) {
			if(!is_null($val)) {
				if($assoc) {
					$json .= "\"$key\":".json_encode($val).",";
				}else {
					$json .= json_encode($val).",";
				}
			}
		}
		if(strlen($json)>1) {// 加上判断 防止空数组
			$json  = substr($json,0,-1);
		}
		$json .= $assoc ? '}' : ']' ;
		return $json;
	}
}
if (!function_exists('json_decode')) {
	function json_decode($json,$assoc=false)
	{
		// 目前不支持二维数组或对象
		$begin  =  substr($json,0,1) ;
		if(!in_array($begin,array('{','[')))
		// 不是对象或者数组直接返回
		return $json;
		$parse = substr($json,1,-1);
		$data  = explode(',',$parse);
		if($flag = $begin =='{' ) {
			// 转换成PHP对象
			$result   = new stdClass();
			foreach($data as $val) {
				$item    = explode(':',$val);
				$key =  substr($item[0],1,-1);
				$result->$key = json_decode($item[1],$assoc);
			}
			if($assoc)
			$result   = get_object_vars($result);
		}else {
			// 转换成PHP数组
			$result   = array();
			foreach($data as $val)
			$result[]  =  json_decode($val,$assoc);
		}
		return $result;
	}
}
if (!function_exists('property_exists')) {
	/**
	 +----------------------------------------------------------
	 * 判断对象的属性是否存在 PHP5.1.0以上已经定义
	 +----------------------------------------------------------
	 * @param object $class 对象实例
	 * @param string $property 属性名称
	 +----------------------------------------------------------
	 * @return boolen
	 +----------------------------------------------------------
	 */
	function property_exists($class, $property) {
		if (is_object($class))
		$class = get_class($class);
		return array_key_exists($property, get_class_vars($class));
	}
}
?>